﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
           
            var connStr = "server=.;database=Admin001;uid=sa;pwd=123456";

            var sqlCon = new SqlConnection(connStr);
            sqlCon.Open();


            var cmdStr = "select * from Student";

            SqlCommand sql = new SqlCommand(cmdStr, sqlCon);

            //一
            var reader = sql.ExecuteReader();
            Console.WriteLine("{0}\t{1}\t{2}\t{3}", "id", "StudentName", "Sex", "CreateTime");
            while (reader.Read())
            {
                Console.WriteLine("{0}\t{1}\t\t{2}\t{3}", reader[0], reader[1], reader[2], reader[3]);
            }
            sqlCon.Close();


            Console.WriteLine();

            //二
            DataTable table = new DataTable();
            SqlDataAdapter sqlData = new SqlDataAdapter(cmdStr, sqlCon);
            sqlData.Fill(table);
            foreach (DataRow item in table.Rows)
            {
                Console.WriteLine("{0}\t{1}\t\t{2}\t{3}", item[0], item[1], item[2], item[3]);
            }
            sqlCon.Close();


            //sql.CommandText = "insert into Student(StudentName,Sex)values('小二','男')";
            //sql.ExecuteNonQuery();

            //sql.CommandText = "update  Student set StudentName='小二',Sex='女' where Id=4";
            //sql.ExecuteNonQuery();

            //sql.CommandText = "delete from Student where StudentName='小二'";
            //sql.ExecuteNonQuery();            
        }
    }
}
